package com.jinkaimen.goodcash.service;

import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.ht.baselibs.application.HTApplication;
import com.ht.baselibs.bean.BaseGetBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.utils.LogUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.constant.ConstantPool;
import com.jinkaimen.goodcash.ui.activity.TransactionHistoryActivity;
import com.jinkaimen.goodcash.utils.NotificationUtils;
import com.jinkaimen.goodcash.utils.VoiceUtils;

import de.greenrobot.event.EventBus;

public class GetuiService extends GTIntentService {

    public static final String GETUI_CLIEND_ID = "getui_cliend_id";

    public static final int AUTHOR_SUCCESS = 100;
    public static final int AUTHOR_FAIL = 101;

    public static final int GET_MONEY = 200;

    @Override
    public void onReceiveServicePid(Context context, int i) {

    }

    @Override
    public void onReceiveClientId(final Context context, String clientid) {
        LogUtils.error("clientid:"+clientid+"   推送");
        SharedPreferencesUtils.put(GETUI_CLIEND_ID, clientid);
        Intent downloadIntent = new Intent(context, UploadClientIdSevice.class);
        context.startService(downloadIntent);
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        String taskid = msg.getTaskId();
        String messageid = msg.getMessageId();
        PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
        byte[] payload = msg.getPayload();
        if (payload != null){
            String data = new String(payload);
            LogUtils.error(data+"   推送");
            BaseGetBean baseGetBean = JSON.parseObject(data, BaseGetBean.class);
            Intent intent = null;
            switch (baseGetBean.resultCode){
                case GET_MONEY:
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_BALANCE_CHANGE));
                    intent = new Intent(context, TransactionHistoryActivity.class);
                    NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.resultMessage, intent, false);
                    int notificationType = SharedPreferencesUtils.getInt(ConstantPool.NOTIFICATION_TYPE, 0);
                    if (notificationType == 0){
                        VoiceUtils.playDetails(baseGetBean.resultMessage.split("，")[0]);
                    } else if (notificationType == 1){
                        VoiceUtils.play();
                    }
                    break;
            }
        }

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b) {

    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }
}
